-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaf-accounts
executable file
·230 lines (211 loc) · 9.36 KB
/
af-accounts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python
# vim:set ft=python ts=4 sw=4 et fileencoding=utf-8:
# af-accounts -- Command-line tool to manage accounts on a Ricoh Aficio 1075.
#
# Copyright (C) 2008, 2010 Fabian Knittel <[email protected]>
#
# 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, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
from optparse import OptionParser
from aficio1075 import accounts
from aficio1075 import config_utils
from ConfigParser import SafeConfigParser
import sys
import codecs
plain_stdout = sys.stdout
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
plain_stderr = sys.stderr
sys.stderr = codecs.getwriter('UTF-8')(sys.stderr)
USER_CODE_SECTION = 'user_code'
def show_acct(acct):
if acct.restrict is not None:
perms = []
if acct.restrict.grant_copy:
perms.append('copy')
if acct.restrict.grant_printer:
perms.append('print')
if acct.restrict.grant_scanner:
perms.append('scan')
if acct.restrict.grant_storage:
perms.append('storage')
perm_str = ', '.join(perms)
else:
perm_str = '<unknown perms>'
print u'%5u %20s (%s)' % (acct.user_code, acct.name, perm_str)
print u' stats (A4, A3): copy: %4d, %4d print: %4d, %4d ' \
u'scan: %4d, %4d' % (acct.stats.copy_a4, acct.stats.copy_a3,
acct.stats.print_a4, acct.stats.print_a3, acct.stats.scan_a4,
acct.stats.scan_a3)
def dump_acct(cf, acct):
section = '%s %d' % (USER_CODE_SECTION, acct.user_code)
cf.add_section(section)
for n, v in (
('name', acct.name),
('restrict.grant_copy', acct.restrict.grant_copy),
('restrict.grant_printer', acct.restrict.grant_printer),
('restrict.grant_scanner', acct.restrict.grant_scanner),
('restrict.grant_storage', acct.restrict.grant_storage),
('stats.copy_a4', acct.stats.copy_a4),
('stats.copy_a3', acct.stats.copy_a3),
('stats.print_a4', acct.stats.print_a4),
('stats.print_a3', acct.stats.print_a3),
('stats.scan_a4', acct.stats.scan_a4),
('stats.scan_a3', acct.stats.scan_a3)):
str_value = unicode(v).encode('utf-8')
cf.set(section, n, str_value)
def load_acct(cf, user_code):
section = '%s %d' % (USER_CODE_SECTION, user_code)
acct = accounts.User(user_code, cf.get(section, 'name').decode('utf-8'))
acct.restrict = accounts.UserRestrict()
acct.restrict.grant_copy = cf.getboolean(section, 'restrict.grant_copy')
acct.restrict.grant_printer = cf.getboolean(section,
'restrict.grant_printer')
acct.restrict.grant_scanner = cf.getboolean(section,
'restrict.grant_scanner')
acct.restrict.grant_storage = cf.getboolean(section,
'restrict.grant_storage')
acct.stats = accounts.UserStatistics()
acct.stats.copy_a4 = cf.getint(section, 'stats.copy_a4')
acct.stats.copy_a3 = cf.getint(section, 'stats.copy_a3')
acct.stats.print_a4 = cf.getint(section, 'stats.print_a4')
acct.stats.print_a3 = cf.getint(section, 'stats.print_a3')
acct.stats.scan_a4 = cf.getint(section, 'stats.scan_a4')
acct.stats.scan_a3 = cf.getint(section, 'stats.scan_a3')
return acct
def main():
valid_modes = [ 'disable', 'delete', 'enable', 'add', 'show', 'reset',
'dump', 'load' ]
usage = "usage: %prog [options] (" + "|".join(valid_modes) + ")"
parser = OptionParser(usage=usage)
parser.add_option("--config", action = "store", dest = "config_file",
help = "Path of configuration file",
default = config_utils.CONFIG_PATH)
parser.add_option("--hostname", action = "store", dest = "hostname",
help = "Hostname of the Aficio printer")
parser.add_option("--passwd", action = "store", dest = "passwd",
help = "Password for the Aficio printer")
parser.add_option("--user-code", action = "store", type = "int",
dest = "user_code",
help = "The user code (mandatory for all commands except show, dump and load)")
parser.add_option("--user-name", action = "store", dest = "name",
help = "The user's name")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
cf = SafeConfigParser()
cf.read(options.config_file)
if options.hostname is not None:
cf.set('printer', 'hostname', options.hostname)
if options.passwd is not None:
cf.set('printer', 'passwd', options.passwd)
if not cf.has_section('printer') or \
not cf.has_option('printer', 'hostname'):
parser.error("hostname not specified")
if not cf.has_section('printer') or \
not cf.has_option('printer', 'passwd'):
parser.error("passwd not specified")
mode = args[0]
if mode not in valid_modes:
parser.error("unknown mode '%s'" % mode)
if mode not in ('show', 'dump', 'load') and options.user_code is None:
parser.error("Expected option --user-code")
um = accounts.UserMaintSession(host = cf.get('printer', 'hostname'),
passwd = cf.get('printer', 'passwd'), retry_busy = True)
# Default restrictions / permissions.
acct_restr = accounts.UserRestrict(grant_copy = True, grant_printer = True,
grant_scanner = True)
if mode == 'disable':
# Disable account
acct = um.get_user_info(options.user_code)
if acct.restrict.has_any_permissions():
print u"Disabling user %s (%u)" % (acct.name, acct.user_code)
acct.restrict.revoke_all()
um.set_user_info(acct)
else:
print u"User %s (%u) already disabled" % (acct.name, acct.user_code)
elif mode == 'delete':
# Delete account
acct = um.get_user_info(options.user_code)
if acct.stats.is_zero():
print u"Removing user %s (%u)" % (acct.name, acct.user_code)
um.delete_user(acct.user_code)
else:
print u"User %s (%u) has non-zero counters. Cannot delete." % (
acct.name, acct.user_code)
elif mode == 'enable':
# Enable account
acct = um.get_user_info(options.user_code)
if not acct.restrict.has_any_permissions():
print u"Enabling user %s (%u)" % (acct.name, acct.user_code)
acct.restrict = acct_restr
um.set_user_info(acct)
else:
print u"User %s (%u) already enabled" % (acct.name, acct.user_code)
elif mode == 'reset':
# Reset the counters for the account by removing and adding it.
acct = um.get_user_info(options.user_code)
print u"Resetting user %s (%u)" % (acct.name, acct.user_code)
um.delete_user(acct.user_code)
um.add_user(acct)
elif mode == 'add':
# Add new account
if options.name is None:
parser.error("Expected option --user-name")
if len(options.name) > accounts.User.MAX_NAME_LEN:
parser.error("User name must not exceed %d characters" %
accounts.User.MAX_NAME_LEN)
user_name = unicode(options.name, 'utf-8')
print u"Adding user %s (%u)" % (user_name, options.user_code)
acct = accounts.User(options.user_code, user_name, acct_restr)
um.add_user(acct)
elif mode == 'show':
# Show account
if options.user_code is not None:
acct = um.get_user_info(options.user_code)
show_acct(acct)
else:
for acct in um.get_user_infos():
show_acct(acct)
elif mode == 'dump':
if options.user_code is not None:
parser.error("filtering by user code is not supported for dump")
dump_cf = SafeConfigParser()
for acct in um.get_user_infos():
dump_acct(dump_cf, acct)
dump_cf.write(plain_stdout)
elif mode == 'load':
load_cf = SafeConfigParser()
load_cf.readfp(sys.stdin)
# TODO: Allow (re-)adding users.
# TODO: Delete not-listed users.
if options.user_code is not None:
print u"Loading data for individual user code %d ..." % \
(options.user_code)
acct = load_acct(load_cf, options.user_code)
um.set_user_info(acct)
else:
print u"Loading all user data ..."
SECTION_PREFIX = USER_CODE_SECTION + ' '
user_codes = [int(section_name[len(SECTION_PREFIX):]) \
for section_name in load_cf.sections() \
if section_name.startswith(SECTION_PREFIX)]
for user_code in user_codes:
acct = load_acct(load_cf, user_code)
show_acct(acct)
try:
um.set_user_info(acct)
except accounts.UserMaintError, ex:
print ex
if __name__ == '__main__':
main()