/usr/share/pyshared/d_rats/sessions/rpc.py is in d-rats 0.3.3-3ubuntu1.
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | #!/usr/bin/python
#
# Copyright 2008 Dan Smith <dsmith@danplanet.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 3 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, see <http://www.gnu.org/licenses/>.
import time
import datetime
import os
import glob
import sys
import gobject
from d_rats import ddt2, signals, emailgw, wl2k
# This feels wrong
from d_rats.ui import main_events
from d_rats.sessions import base, stateless
from d_rats.version import DRATS_VERSION
from d_rats.utils import log_exception
ASCII_FS = "\x1C"
ASCII_GS = "\x1D"
ASCII_RS = "\x1E"
ASCII_US = "\x1F"
class UnknownRPCCall(Exception):
pass
def encode_dict(source):
elements = []
for k, v in source.items():
if not isinstance(k, str):
raise Exception("Cannot encode non-string dict key")
if not isinstance(v, str):
raise Exception("Cannoy encode non-string dict value")
elements.append(k + ASCII_US + v)
return ASCII_RS.join(elements)
def decode_dict(string):
result = {}
if not string:
return result
elements = string.split(ASCII_RS)
for element in elements:
try:
k, v = element.split(ASCII_US)
except ValueError:
raise Exception("Malformed dict encoding")
result[k] = v
return result
class RPCJob(gobject.GObject):
__gsignals__ = {
"state-change" : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE,
(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT)),
}
STATES = ["complete", "timeout", "running"]
def __init__(self, dest, desc):
gobject.GObject.__init__(self)
self.__dest = dest
self.__desc = desc
self._args = {}
def get_dest(self):
return self.__dest
def get_desc(self):
return self.__desc
def set_state(self, state, result={}):
if not isinstance(result, dict):
raise Exception("Value of result property must be dict")
if state in self.STATES:
gobject.idle_add(self.emit, "state-change", state, result)
else:
raise Exception("Invalid status `%s'" % state)
def unpack(self, raw):
self._args = {}
if not raw:
self._args = {}
else:
self._args = decode_dict(raw)
def pack(self):
return encode_dict(self._args)
def do(self, rpcactions):
return {"rc" : "Unsupported Job Type"}
class RPCFileListJob(RPCJob):
def set_file_list(self, list):
self._args = {}
for item in list:
self._args[item] = ""
def get_file_list(self):
return self._args.keys()
def do(self, rpcactions):
return rpcactions.RPC_file_list(self)
class RPCFormListJob(RPCJob):
def get_form_list(self):
return []
def do(self, rpcactions):
return rpcactions.RPC_form_list(self)
class RPCPullFileJob(RPCJob):
def set_file(self, filename):
self._args = {"fn" : filename}
def get_file(self):
return self._args.get("fn", None)
def do(self, rpcactions):
return rpcactions.RPC_file_pull(self)
class RPCDeleteFileJob(RPCJob):
def set_file(self, filename):
self._args["fn"] = filename
def set_pass(self, passwd):
self._args["passwd"] = passwd
def get_file(self):
return self._args.get("fn", None)
def get_pass(self):
return self._args.get("passwd", "")
def do(self, rpcactions):
return rpcactions.RPC_file_delete(self)
class RPCPullFormJob(RPCJob):
def set_form(self, form):
self._args = {"fn" : form}
def get_form(self):
return self._args.get("fn", None)
def do(self, rpcactions):
return rpcactions.RPC_form_pull(self)
class RPCPositionReport(RPCJob):
def set_station(self, station):
self._args = {"st" : station}
def get_station(self):
return self._args.get("st", "ERROR")
def do(self, rpcactions):
return rpcactions.RPC_pos_report(self)
class RPCGetVersion(RPCJob):
def do(self, rpcactions):
return rpcactions.RPC_get_version(self)
class RPCCheckMail(RPCJob):
def do(self, rpcactions):
return rpcactions.RPC_check_mail(self)
def set_account(self, host, user, pasw, port, ssl):
self._args = {"host" : host,
"user" : user,
"pasw" : pasw,
"port" : port,
"ssl" : ssl,
}
def get_account(self):
return self._args["host"], self._args["user"], self._args["pasw"], \
int(self._args["port"]), self._args["ssl"] == "True"
class RPCSession(gobject.GObject, stateless.StatelessSession):
type = base.T_RPC
T_RPCREQ = 0
T_RPCACK = 1
def __init__(self, *args, **kwargs):
gobject.GObject.__init__(self)
try:
self.__rpcactions = kwargs["rpcactions"]
del kwargs["rpcactions"]
except KeyError:
raise Exception("RPCSession requires RPCActionSet")
stateless.StatelessSession.__init__(self, *args, **kwargs)
self.__jobs = {}
self.__jobq = []
self.__jobc = 0
self.__t_retry = 30
self.__enabled = True
gobject.timeout_add(1000, self.__worker)
self.handler = self.incoming_data
def notify(self):
pass
def __decode_rpccall(self, frame):
jobtype, args = frame.data.split(ASCII_GS)
# FIXME: Make this more secure
if not (jobtype.isalpha() and jobtype.startswith("RPC")):
raise UnknownRPCCall("Unknown call `%s'" % jobtype)
job = eval("%s('%s', 'New job')" % (jobtype, frame.s_station))
job.unpack(args)
return job
def __encode_rpccall(self, job):
return "%s%s%s" % (job.__class__.__name__, ASCII_GS, job.pack())
def __get_seq(self):
self.__jobc += 1
return self.__jobc
def __job_to_frame(self, job, id):
frame = ddt2.DDT2EncodedFrame()
frame.type = self.T_RPCREQ
frame.seq = id
frame.data = self.__encode_rpccall(job)
frame.d_station = job.get_dest()
return frame
def __send_job_status(self, id, station, state, result):
frame = ddt2.DDT2EncodedFrame()
frame.type = self.T_RPCACK
frame.seq = id
frame.data = result
frame.d_station = station
return frame
def __job_state(self, job, state, _result, id):
print "Job state: %s for %i: %s" % (state, id, _result)
if state == "running":
return
result = encode_dict(_result)
f = self.__send_job_status(id, job.get_dest(), state, result)
self._sm.outgoing(self, f)
def incoming_data(self, frame):
if frame.type == self.T_RPCREQ:
try:
job = self.__decode_rpccall(frame)
except UnknownRPCCall, e:
print "Unable to execute RPC from %s: %s" % (frame.s_station, e)
return
job.connect("state-change", self.__job_state, frame.seq)
result = job.do(self.__rpcactions)
if result is not None:
job.set_state("complete", result)
elif frame.type == self.T_RPCACK:
if self.__jobs.has_key(frame.seq):
ts, att, job = self.__jobs[frame.seq]
del self.__jobs[frame.seq]
job.set_state("complete", decode_dict(frame.data))
else:
print "Unknown job %i" % frame.seq
else:
print "Unknown RPC frame type %i" % frame.type
def __send_job(self, job, id):
print "Sending job `%s' to %s" % (job.get_desc(), job.get_dest())
frame = self.__job_to_frame(job, id)
job.frame = frame
self._sm.outgoing(self, frame)
print "sent"
def __worker(self):
for id, (ts, att, job) in self.__jobs.items():
if job.frame and not job.frame.sent_event.isSet():
# Reset timer until the block is sent
self.__jobs[id] = (time.time(), att, job)
elif (time.time() - ts) > self.__t_retry:
print "Cancelling job %i due to timeout" % id
del self.__jobs[id]
job.set_state("timeout")
return True
def submit(self, job):
id = self.__get_seq()
self.__send_job(job, id)
self.__jobs[id] = (time.time(), 0, job)
def stop(self):
self.__enabled = False
class RPCActionSet(gobject.GObject):
__gsignals__ = {
"rpc-send-file" : signals.RPC_SEND_FILE,
"rpc-send-form" : signals.RPC_SEND_FORM,
"get-message-list" : signals.GET_MESSAGE_LIST,
"get-current-position" : signals.GET_CURRENT_POSITION,
"user-send-chat" : signals.USER_SEND_CHAT,
"event" : signals.EVENT,
"register-object" : signals.REGISTER_OBJECT,
"form-received" : signals.FORM_RECEIVED,
"form-sent" : signals.FORM_SENT,
}
_signals = __gsignals__
def __init__(self, config, port):
self.__config = config
self.__port = port
gobject.GObject.__init__(self)
def __proxy_emit(self, signal):
def handler(obj, *args):
print "Proxy emit %s: %s" % (signal, args)
gobject.idle_add(self.emit, signal, *args)
return handler
def RPC_pos_report(self, job):
result = {}
mycall = self.__config.get("user", "callsign")
rqcall = job.get_station()
if rqcall == mycall or rqcall == ".":
rqcall = None
try:
fix = self.emit("get-current-position", rqcall)
result["rc"] = "OK"
except Exception, e:
print "Exception while getting position of %s: " % rqcall
log_exception()
fix = None
result["rc"] = "No data for station '%s'" % job.get_station()
if fix:
self.emit("user-send-chat",
"CQCQCQ", self.__port, fix.to_NMEA_GGA(), True)
print "[RPC] Position request for `%s'" % job.get_station()
return result
def RPC_file_list(self, job):
result = {}
dir = self.__config.get("prefs", "download_dir")
files = glob.glob(os.path.join(dir, "*.*"))
for fn in files:
if os.path.isdir(fn):
continue
size = os.path.getsize(fn)
if size < 1024:
units = "B"
else:
size >>= 10
units = "KB"
ds = datetime.datetime.fromtimestamp(os.path.getmtime(fn))
fn = os.path.basename(fn)
result[fn] = "%i %s (%s)" % (size,
units,
ds.strftime("%Y-%m-%d %H:%M:%S"))
event = main_events.Event(None, job.get_dest() + " " + \
_("Requested file list"))
self.emit("event", event)
return result
def RPC_form_list(self, job):
result = {}
forms = self.emit("get-message-list", "CQCQCQ")
for subj, stamp, filen in forms:
ts = time.localtime(stamp)
result[filen] = "%s/%s" % (subj,
time.strftime("%b-%d-%Y %H:%M:%S", ts))
event = main_events.Event(None, job.get_dest() + " " + \
_("Requested message list"))
self.emit("event", event)
return result
def RPC_file_pull(self, job):
result = {}
if not self.__config.getboolean("prefs", "allow_remote_files"):
result["rc"] = "Remote file transfers not enabled"
return result
dir = self.__config.get("prefs", "download_dir")
path = os.path.join(dir, job.get_file())
print "Remote requested %s" % path
if os.path.exists(path):
result["rc"] = "OK"
self.emit("rpc-send-file",
job.get_dest(), self.__port, path, job.get_file())
else:
result["rc"] = "File not found"
event = main_events.Event(None, job.get_dest() + " " + \
_("Requested file %s") % job.get_file())
self.emit("event", event)
return result
def RPC_file_delete(self, job):
result = {}
_permlist = self.__config.get("settings", "delete_from")
try:
permlist = _permlist.upper().split(",")
except Exception:
result["rc"] = "Access list not properly configured"
return result
if job.get_dest().upper() not in permlist:
result["rc"] = "Access denied for %s" % job.get_dest()
return result
passwd = self.__config.get("settings", "remote_admin_passwd")
if passwd and job.get_pass() != passwd:
result["rc"] = "Access denied (Incorrect Password)"
return result
if "/" in job.get_file():
result["rc"] = "Access denied (file contains slash)"
return result
path = os.path.join(self.__config.get("prefs", "download_dir"),
job.get_file())
if not os.path.exists(path):
result["rc"] = "File not found (%s)" % job.get_file()
return result
try:
os.remove(path)
result["rc"] = "File %s deleted" % job.get_file()
except Exception, e:
result["rc"] = "Unable to delete %s: %s" % (job.get_file(), e)
return result
def RPC_form_pull(self, job):
result = {}
forms = self.emit("get-message-list", "CQCQCQ")
result["rc"] = "Form not found"
for subj, stamp, filen in forms:
if filen == job.get_form():
fname = os.path.join(self.__config.platform.config_dir(),
"messages", filen)
if os.path.exists(fname):
result["rc"] = "OK"
self.emit("rpc-send-form",
job.get_dest(), self.__port, fname, subj)
break
event = main_events.Event(None, job.get_dest() + " " + \
_("Requested message %s") % subj)
self.emit("event", event)
return result
def RPC_get_version(self, job):
result = {}
result["version"] = DRATS_VERSION
result["os"] = self.__config.platform.os_version_string()
result["pyver"] = ".".join([str(x) for x in sys.version_info[:3]])
try:
import gtk
result["pygtkver"] = ".".join([str(x) for x in gtk.pygtk_version])
result["gtkver"] = ".".join([str(x) for x in gtk.gtk_version])
except ImportError:
result["pygtkver"] = result["gtkver"] = "Unknown"
return result
def RPC_check_mail(self, job):
def check_done(mt, success, message, job):
result = { "rc" : success and "0" or "-1",
"msg" : message }
job.set_state("complete", result)
event = main_events.Event(None,
"%s %s: %s" % (_("Checking mail for"),
job.get_dest(),
message))
self.emit("event", event)
args = job.get_account() + (job.get_dest(),)
if args[0] == "@WL2K":
if self.__config.getboolean("prefs", "msg_allow_wl2k"):
mt = wl2k.WinLinkThread(self.__config,
job.get_dest(),
args[1])
mt.connect("event", self.__proxy_emit("event"))
mt.connect("form-received", self.__proxy_emit("form-received"))
mt.connect("form-sent", self.__proxy_emit("form-sent"))
else:
return {"rc" : "False",
"msg" : "WL2K gateway is disabled"}
else:
if self.__config.getboolean("prefs", "msg_allow_pop3"):
mt = emailgw.CoercedMailThread(self.__config, *args)
else:
return {"rc" : "False",
"msg" : "POP3 gateway is disabled"}
self.emit("register-object", mt)
mt.connect("mail-thread-complete", check_done, job)
mt.start()
event = main_events.Event(None,
"%s %s" % (job.get_dest(),
_("requested a mail check")))
self.emit("event", event)
|