Repository navigation
Expand file tree
/
Copy pathcgtools
More file actions
executable file
·228 lines (203 loc) · 8.51 KB
/
Copy pathcgtools
File metadata and controls
executable file
·228 lines (203 loc) · 8.51 KB
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
#!/opt/local/bin/ruby1.9
# -*- coding: utf-8 -*-
require 'thor'
require 'pp'
require 'communigate/cli'
require "net/imap"
class CgTools < Thor
DEF_DOMAIN = 'company.com'
CGATE_HOST = 'cgate.company.com'
CGATE_USER = '[email protected]'
CGATE_PASS = 'verysecret'
MAX_OBJECTS = 1000
desc "getdetail ADDR [-(grs)]", "get detailed info on ADDR on cgate server (with cgate API)"
method_option :groups, :aliases => '-g', :type => :boolean, :desc => 'groups/lists'
method_option :roster, :aliases => '-r', :type => :boolean, :desc => 'roster'
method_option :settings, :aliases => '-s', :type => :boolean, :desc => 'dump all account settings'
# method_option :boxes, :aliases => '-b', :type => :boolean, :desc => 'mailboxes'
# method_option :info, :aliases => '-i', :type => :string, :desc => 'dump account state information'
def getdetail(short_addr)
addr = "#{short_addr}@#{DEF_DOMAIN}"
cli = CommuniGate::Cli.new(:hostname => CGATE_HOST,
:username => CGATE_USER,
:password => CGATE_PASS)
begin
begin
settings = cli.getaccountsettings(addr)
rescue CommuniGate::GeneralException => exp
if exp.message == "The CG server returned an error: '513 unknown user account'"
STDERR.puts "no such account"
alike_names = cli.listdomainobjects(DEF_DOMAIN, "FILTER", short_addr, "50",
'ACCOUNTS', 'ALIASES', 'FORWARDERS')[1]
if alike_names.has_key? short_addr
# ref: 'macnt' for ACCOUNTS, [target] for ALIASES, [[target]] for FORWARDER
ref = alike_names[short_addr][0]
case ref
when String then STDERR.puts "... it is actually an alias for #{ref}"
when Array then STDERR.puts "... it is actually a forwarder to #{ref[0]}"
else STDERR.puts "it is something strange that cannot happen (#{ref})"
end
elsif cli.listgroups(DEF_DOMAIN).include? short_addr
STDERR.puts "... it is actually a group"
gr = cli.getgroup(addr)
puts "RealName: #{gr['RealName']}"
if gr.has_key? 'Members'
puts "Members:"
gr['Members'].each {|m| puts " " + m}
end
elsif !alike_names.empty?
STDERR.puts "there are some names like it"
alike_names.each_key {|k| STDERR.puts k}
else
STDERR.puts "and nothing like it found"
exit 1
end
else
STDERR.puts "error: #{exp.message}"
end
exit 1
end
# basic info (or full dump with '--settings')
settings.delete('Password')
keys_to_print = options.settings? ? settings.keys : %w(RealName Title ou EmpNumber)
keys_to_print.each do |k|
printf("%-10s: %-s\n", k, settings[k]) if settings.has_key? k
end
# aliases: print always
aliases = cli.getaccountaliases(addr)
puts "aliases : " + aliases.join(', ') unless aliases.empty?
# forwarders: must look up membership (one round-trip)
forwarders = cli.findforwarders(DEF_DOMAIN, 'TO', short_addr)
puts "forwarders: " + forwarders.join(', ') unless forwarders.empty?
# lists/groups: must test each one separately; take aliases in account too
if options.groups?
groups = []
cli.listgroups(DEF_DOMAIN).each do |gname|
ginfo = cli.getgroup("#{gname}@#{DEF_DOMAIN}")
next unless ginfo.has_key? "Members"
# mention forwarders in group names
if ginfo["Members"].include? short_addr
if !(group_forwarders = cli.findforwarders(DEF_DOMAIN, 'TO', gname)).empty?
groups << gname + " (aka #{group_forwarders.join(', ')})"
else
groups << gname
end
end
# lets think that forwarders could not be group members for now :)
aliases.each {|a| groups << "#{gname} (as #{a})" if ginfo["Members"].include? a}
end
puts "groups : " + groups.join(', ') unless groups.empty?
end
# roster: our firends (lets think that friendship is always mutual)
if options.roster?
friends = cli.roster(addr, {'what' => 'List'}).keys.map {|v| v.gsub("@#{DEF_DOMAIN}", '')}
puts "friends : " + friends.join(', ') unless friends.empty?
#friend_ofs = []
#cli.listaccounts(DEF_DOMAIN).each_key do |u|
# friend_ofs << u if cli.roster("#{u}@#{DEF_DOMAIN}", {'what' => 'List'}).keys.include? addr
#end
#puts "friend of : " + friend_ofs.join(', ') unless friend_ofs.empty?
end
rescue CommuniGate::GeneralException, CommuniGate::DataException => exp
STDERR.puts "error: #{exp.message}"
ensure
cli.disconnect()
end
# # all info: long
# cli.getaccountinfo(''[email protected]')
# # boxes
# cli.listmailboxes('[email protected]').keys.each {|b| puts Net::IMAP.decode_utf7(b)}
# cli.getmailboxinfo('[email protected]', "MAILBOX", "INBOX")
end
desc "deluser ADDR", "del ADDR record from cgate server (including groups and rosters)"
def delnew(short_addr)
addr = "#{short_addr}@#{DEF_DOMAIN}"
cli = CommuniGate::Cli.new(:hostname => CGATE_HOST,
:username => CGATE_USER,
:password => CGATE_PASS)
begin
aliases = cli.getaccountaliases(addr)
puts "aliases: " + aliases.join(', ') unless aliases.empty?
cli.listgroups(DEF_DOMAIN).each do |gname|
gname_full = "#{gname}@#{DEF_DOMAIN}"
ginfo = cli.getgroup(gname_full)
next unless ginfo.has_key? "Members"
changed = false
if ginfo["Members"].include? short_addr
puts "- group: #{gname}"
ginfo["Members"].delete(short_addr)
changed = true
end
# alias could be a member of some list too (lets neglect forwarders for now)
aliases.each do |alias_short_addr|
if ginfo["Members"].include? alias_short_addr
puts "- group: #{gname} (as #{alias_short_addr})"
ginfo["Members"].delete(alias_short_addr)
changed = true
end
end
cli.setgroup(gname_full, ginfo) if changed
end
# assume that aliases could not be friends
cli.listaccounts(DEF_DOMAIN).each_key do |other_short_addr|
other_addr = "#{other_short_addr}@#{DEF_DOMAIN}"
if cli.roster(other_addr, {'what' => 'List'}).keys.include? addr
puts "- friend of: #{other_short_addr}"
cli.roster(other_addr, {'what' => 'remove', 'peer' => addr})
end
end
if !(forwarders = cli.findforwarders(DEF_DOMAIN, 'TO', short_addr)).empty?
puts "forwarders: " + forwarders.join(', ')
forwarders.each {|f| cli.deleteforwarder("#{f}@#{DEF_DOMAIN}")}
end
cli.deleteaccount(addr)
rescue CommuniGate::GeneralException => exp
STDERR.puts "cgate error: #{exp.message}"
ensure
cli.disconnect()
end
puts "#{addr} deleted"
end
desc "blacklist [-c]", "get or clear a list of blacklisted IPs from server"
method_option :clear, :aliases => '-c', :type => :boolean, :desc => 'clear list, enabling them all back'
def blacklist()
cli = CommuniGate::Cli.new(:hostname => CGATE_HOST,
:username => CGATE_USER,
:password => CGATE_PASS)
begin
# CSV like "[10.4.18.4]-830, ...", number is remaining seconds
blacklist = cli.getTempBlacklistedIPs()
if blacklist.empty?
puts "no blacklisted IPs"
else
puts blacklist
if options.clear?
# clear by setting remaining time to 1
clist = blacklist.gsub(/-\d+/, "-1")
cli.setTempBlacklistedIPs(clist)
puts "cleared"
end
end
rescue CommuniGate::GeneralException => exp
STDERR.puts "cgate error: #{exp.message}"
ensure
cli.disconnect()
end
end
desc "kill ADDR", "kill all sessions for account"
def kill(short_addr)
addr = "#{short_addr}@#{DEF_DOMAIN}"
begin
cli = CommuniGate::Cli.new(:hostname => LdapDirs::Cgate_new[:host],
:username => LdapDirs::Cgate_new[:user],
:password => LdapDirs::Cgate_new[:pass])
cli.killaccountsessions(addr)
puts "killed"
rescue CommuniGate::GeneralException => exp
STDERR.puts "cgate error: #{exp.message}"
ensure
cli.disconnect()
end
end
end
CgTools.start(ARGV)