1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import time
17 import sys
18 from spacewalk.common.rhnLog import log_debug
19 from spacewalk.server import rhnSQL
20
21 try:
22 import jabber_lib
23 except ImportError:
24 from osad import jabber_lib
25
26 -class Client(jabber_lib.JabberClient):
31
32
33 - def start(self, username, password, resource):
44
46 if not isinstance(jid, jabber_lib.jabber.JID) or jid.resource:
47 return jid
48 return jabber_lib.jabber.JID(str(jid) + '/' + resource)
49
52
54
55 jid = stanza.getFrom()
56 if jid is None:
57 log_debug(3, 'no from')
58 return None
59 jid = str(self._fix_jid(jid))
60
61 xes = stanza.getTags('x')
62 for x in xes:
63 if x.getNamespace() != jabber_lib.NS_RHN_SIGNED:
64 continue
65 break
66 else:
67 log_debug(1, "No signature node found in stanza")
68 return None
69
70 x_client_id = x.getAttr('client-id')
71
72 row = lookup_client_by_name(x_client_id)
73 if not row:
74 log_debug(3, 'no client found', x_client_id)
75 if self.debug_level > 5:
76 raise Exception(1)
77 return None
78 shared_key = row['shared_key']
79 timestamp = x.getAttr('timestap')
80 serial = x.getAttr('serial')
81 action = x.getAttr('action')
82
83 if actions and action not in actions:
84 log_debug(1, "action %s not allowed" % action)
85 return None
86
87 attrs = {
88 'client-id' : x_client_id,
89 'timestamp' : x.getAttr('timestamp'),
90 'serial' : x.getAttr('serial'),
91 'action' : x.getAttr('action'),
92 'jid' : jid,
93 }
94 signing_comps = ['client-id', 'timestamp', 'serial', 'action', 'jid']
95 args = [shared_key, self.jid]
96 for sc in signing_comps:
97 args.append(attrs[sc])
98
99 log_debug(4, "Signature args", args)
100 signature = jabber_lib.sign(*args)
101 x_signature = x.getAttr('signature')
102 if signature != x_signature:
103 log_debug(1, "Signatures do not match", signature, x_signature)
104 if self.debug_level > 5:
105 raise Exception(1)
106 return None
107
108 return x
109
111 row = lookup_client_by_jid(jid)
112 if not row:
113 log_debug(3, 'no client found for jid', jid)
114 if self.debug_level > 5:
115 raise Exception(1)
116 return None
117 full_jid = row['jabber_id']
118 shared_key = row['shared_key']
119 attrs = {
120 'timestamp' : int(time.time()),
121 'serial' : self.get_unique_id(),
122 'action' : action,
123 'jid' : self.jid,
124 }
125 signing_comps = ['timestamp', 'serial', 'action', 'jid']
126 args = [shared_key, full_jid]
127 for sc in signing_comps:
128 args.append(attrs[sc])
129
130 log_debug(4, "Signature args", args)
131 attrs['signature'] = jabber_lib.sign(*args)
132
133 x = jabber_lib.jabber.xmlstream.Node('x')
134 x.setNamespace(jabber_lib.NS_RHN_SIGNED)
135 for k, v in attrs.items():
136 x.putAttr(k, v)
137 return x
138
159
166
170
174
175 _query_set_state = rhnSQL.Statement("""
176 update rhnPushClient
177 set state_id = :state_id,
178 last_ping_time = NULL,
179 next_action_time = NULL
180 where jabber_id = :jid
181 """)
186
188 t = rhnSQL.Table('rhnPushClientState', 'label')
189 row = t[state]
190 assert row is not None
191 return row['id']
192
193 _query_update_client_message_received = rhnSQL.Statement("""
194 update rhnPushClient
195 set state_id = :state_id,
196 last_message_time = current_timestamp,
197 last_ping_time = NULL,
198 next_action_time = NULL
199 where jabber_id = :jid
200 """)
207
210
212 client_name = str(client_name)
213 t = rhnSQL.Table('rhnPushClient', 'name')
214 row = t[client_name]
215 if row is None:
216 raise InvalidClientError(client_name)
217 return row
218
220 if not isinstance(jid, jabber_lib.jabber.JID):
221 jid = jabber_lib.jabber.JID(jid)
222
223 if not jid.getResource():
224
225 jid.setResource('osad')
226
227 jid = str(jid)
228 t = rhnSQL.Table('rhnPushClient', 'jabber_id')
229 row = t[jid]
230 if row is None:
231 raise InvalidClientError(jid)
232 return row
233